package com.dhms.vostok1.ui.main.device

import android.os.Bundle
import android.view.LayoutInflater
import android.view.View
import android.view.ViewGroup
import androidx.fragment.app.viewModels
import com.dhms.devicecenter.R
import com.dhms.devicecenter.databinding.FragmentDeviceSelectBottomSheetBinding
import com.dhms.uikit.TitleBottomSheet
import com.dhms.uikit.deviceCategory.DeviceCategoryItem
import com.dhms.uikit.deviceCategory.DeviceCategoryView
import com.dhms.uikit.deviceCategory.DeviceItem
import com.dhms.uikit.select_bottom_sheet.SelectItem
import com.dhms.vostok1.model.DeviceViewModel

class SelectDeviceBottomSheet(
    private val isMultiSelectMode: Boolean = false,
    private val selectedDevices: List<SelectItem>,
    private val onDevicesSelected: (selectedDevices: List<DeviceItem>) -> Unit,
) :
    TitleBottomSheet() {

    private var _binding: FragmentDeviceSelectBottomSheetBinding? = null
    private val binding get() = _binding!!

    private val viewModel: DeviceViewModel by viewModels()

    override fun onCreateView(
        inflater: LayoutInflater, container: ViewGroup?,
        savedInstanceState: Bundle?
    ): View {
        _binding = FragmentDeviceSelectBottomSheetBinding.inflate(inflater, container, false)
        return binding.root
    }

    override fun onViewCreated(view: View, savedInstanceState: Bundle?) {
        super.onViewCreated(view, savedInstanceState)
        binding.deviceViewModel = viewModel
        binding.lifecycleOwner = viewLifecycleOwner
        binding.isMultiSelectMode = isMultiSelectMode
        binding.deviceCategoryView.singleSelection = !isMultiSelectMode
        binding.deviceCategoryView.setSelectDevices(selectedDevices.map {
            DeviceItem(it.id, it.name, 0, true)
        })

        if (isMultiSelectMode) {
            binding.deviceSelectNegativeBtn.setOnClickListener { dismiss() }
            binding.deviceSelectPositiveBtn.setOnClickListener {
                onDevicesSelected(binding.deviceCategoryView.getSelectDevices)
                dismiss()
            }
        }

        binding.deviceCategoryView.setListener(object : DeviceCategoryView.Listener {
            override fun clickCategoryItem(item: DeviceCategoryItem) {
                viewModel.getDeviceItems(item.ct_id)
            }

            //开始搜索数据
            override fun filterDeviceData(name: String?) {
                if (name == null || name.isEmpty()) {
                    binding.deviceCategoryView.attachCategoryData(viewModel.categoryList)
                }
                viewModel.getFilterData(name)
                //默认选中第一个分类
                val category = viewModel.deviceCategoryData.value?.firstOrNull { it.checkable }
                    ?: viewModel.deviceCategoryData.value?.firstOrNull()
                if (category != null) {
                    category.checkable = true
                    viewModel.getDeviceItems(category.ct_id)
                }
            }

            override fun selectDeviceItem(item: DeviceItem) {
                onDevicesSelected(listOf(item))
                dismiss()
            }
        })

        viewModel.deviceCategoryData.observe(viewLifecycleOwner) {
            if (it != null) {
                binding.deviceCategoryView.attachCategoryData(it)
            }
        }

        viewModel.deviceSubData.observe(viewLifecycleOwner) {
            if (it != null) {
                binding.deviceCategoryView.attachSubCategoryData(it, viewModel.currentCategoryID)
            }
        }

        viewModel.getDeviceCategory()
        viewModel.getAllCategoryDevices()
    }

    override fun getTitle(): String {
        return getString(R.string.select_device_title)
    }
}